#include "stdafx.h"
#include "UpdateWindow.h"

CUpdateWindow::CUpdateWindow(bool silent):
CFrogBaseDialog(NULL),
m_hThreadProgress(NULL),
m_DownloadUpdate(FALSE),
m_Silent(silent)
{
	AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);
}

CUpdateWindow::~CUpdateWindow()
{
	if (m_hThreadProgress)
	{
		WaitForSingleObject(m_hThreadProgress, INFINITE);
		CloseHandle(m_hThreadProgress);
	}
}

LRESULT CUpdateWindow::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	HWND hWndProgress = GetDlgItem(IDC_PROGRESS);

	// Sets the Marquee Style				
	::SetWindowLongPtr(hWndProgress, GWL_STYLE, PBS_SMOOTH | PBS_MARQUEE | WS_BORDER | WS_VISIBLE | WS_CHILD); 
	LRESULT lResult = ::SendMessage(hWndProgress, PBM_SETMARQUEE, TRUE, 100);

	DWORD dwThreadId;

	m_hThreadProgress = CreateThread( 
		NULL,              // default security attributes
		0,                 // use default stack size  
		RunCUpdateThread,          // thread function 
		(LPVOID)this,             // argument to thread function 
		0,                 // use default creation flags 
		&dwThreadId);   // returns the thread identifier

	return 0;
}

DWORD WINAPI CUpdateWindow::RunCUpdateThread(LPVOID lpParam)
{
	CUpdateWindow* updateWindow = (CUpdateWindow*)lpParam;

	//Sleep(10000);

	if (updateWindow->m_FilerFrogUpdater.CheckInternetConnection())
	{		
		if (updateWindow->m_FilerFrogUpdater.CheckVersion())
		{		
			::SendMessage(updateWindow->m_hWnd, WM_NO_NEW_UPDATE, 0, 0);

			if (updateWindow->m_DownloadUpdate)
			{
				Sleep(1000);
				updateWindow->m_FilerFrogUpdater.DownloadAndRunInstallation();
			}
		}
		else
		{
			::SendMessage(updateWindow->m_hWnd, WM_FOUND_NEW_UPDATE, 0, 0);		
		}

		::SendMessage(updateWindow->m_hWnd, WM_FINISH_UPDATE, 0, 0);
	}
	else
	{
		::SendMessage(updateWindow->m_hWnd, WM_FINISH_UPDATE, 1, 1);
	}

	return 0;
}

LRESULT CUpdateWindow::OnNoNewUpdate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	wstring versionName = m_FilerFrogUpdater.GetLatestVersionName();
	wstring newVersionMsg = L"New Version Found ("+ versionName + L")! Update to latest version?";

	if (FrogMessageBox(m_hWnd, newVersionMsg.c_str(), L"FilerFrog Automatic Update", MB_YESNO) == IDYES)
	{
		::SetWindowText(GetDlgItem(IDC_STATIC_TITLE), L"Downloading update, please wait...");
		Invalidate();
		m_DownloadUpdate = true;
	}
	else
	{
		EndDialog(0);
	}

	return 0;
}

LRESULT CUpdateWindow::OnFoundUpdate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if (!m_Silent)
	{
		FrogMessageBox(m_hWnd, L"Your computer is running the most recent version of FilerFrog. No update is available at this time.", L"FilerFrog Automatic Update", 0);
	}

	EndDialog(0);
	return 0;
}

LRESULT CUpdateWindow::OnFinishUpdate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if (wParam == 1)
	{
		if (!m_Silent)
		{
			FrogMessageBox(m_hWnd, L"Please make sure you are connected to the internet or try to run Setup again later.", L"FilerFrog Automatic Update", 0);
		}
	}

	EndDialog(0);
	return 0;
}

LRESULT CUpdateWindow::OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	return 0;
}